package com.yiwei.ydd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.api.model.RechargeRecordModel;
import com.yiwei.ydd.util.Arith;
import com.yiwei.ydd.util.DateTime;
import com.yiwei.ydd.util.Spans;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class RechargeRecordAdapter extends ItemAdapter<RechargeRecordModel.DatasBean, RechargeRecordAdapter.DefaultHolder> {

    private final int textSize24;


    private Context context;
    //充值类型（1：快速充值，2：分期充值）
    private String type = "1";

    public void setType(String type) {
        this.type = type;
    }

    public RechargeRecordAdapter(Context context) {
        super(context);
        textSize24 = context.getResources().getDimensionPixelSize(R.dimen.x24);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_recharge_record, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        RechargeRecordModel.DatasBean item = getItem(position);
        holder.txtName.setText(item.oilcard_name + item.oilcard_no);
//（-2：失效，-1：失败，0：待支付，1：完成，2：已付款，3：分期种）
        holder.txtStatus.setText(getStatus(item.status));

        if (item.type.equals("1")) {
            Spanned textOne = Spans.with(context).font("充值金额：" + item.total_money + "元\n", textSize24, 0xff666666).
                    font("应付金额：" + item.total_money + "元\n", textSize24, 0xff666666).
                    font("付款时间：" + DateTime.toDateTime(Long.valueOf(item.pay_date) * 1000), textSize24, 0xff666666).
                    build();
            holder.txtInfo.setText(textOne);


            Spanned text_two = Spans.with(context).font("折扣幅度：" + Arith.mul(Float.valueOf(item.sale), 0.1) + "折\n", textSize24, 0xff666666).
                    font("实付金额：" + item.pay_money + "元\n", textSize24, 0xff666666).
                    build();
            holder.txtInfoRight.setText(text_two);
            holder.layoutBottom.setVisibility(View.GONE);
        } else {
            Spanned textOneSec = Spans.with(context).font("套餐时长：" + item.periods + "个月\n", textSize24, 0xff666666).
                    font("加油包：" + item.coupon_money + "元\n", textSize24, 0xff666666).
                    font("实际付款金额：" + item.pay_money + "元\n", textSize24, 0xff666666).
                    font("付款时间：" + DateTime.toDateTime(Long.valueOf(item.pay_date) * 1000), textSize24, 0xff666666).
                    build();
            holder.txtInfo.setText(textOneSec);


            Spanned text_two_sec = Spans.with(context).font("每月油卡充值：" + item.money + "元\n", textSize24, 0xff666666).
                    font("每月多赠送：" + item.git + "%(" + item.git_value + "元)\n", textSize24, 0xff666666).
                    font("累计到账总额：" + item.total + "元", textSize24, 0xff666666).
                    build();
            holder.txtInfoRight.setText(text_two_sec);
            holder.layoutBottom.setVisibility(View.VISIBLE);
            holder.layoutBottom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onChooseListener != null) {
                        onChooseListener.onClick(item);
                    }
                }
            });
        }
    }

    private String getStatus(String status) {
        //（-2：失效，-1：失败，0：待支付，1：完成，2：已付款，3：分期种）
        switch (status) {
            case "-2":
                return "失效";
            case "-1":
                return "失败";
            case "0":
                return "待支付";
            case "1":
                return "完成";
            case "2":
                return "已付款";
            case "3":
                return "加油中";
            default:
                return "";
        }
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_name)
        TextView txtName;
        @BindView(R.id.txt_status)
        TextView txtStatus;
        @BindView(R.id.txt_info)
        TextView txtInfo;
        @BindView(R.id.view_center)
        View viewCenter;
        @BindView(R.id.txt_info_right)
        TextView txtInfoRight;
        @BindView(R.id.layout_bottom)
        FrameLayout layoutBottom;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(RechargeRecordModel.DatasBean item);
    }
}
